// 1.设计一个图书管理系统，其中包括Book类和Library类。
// Book类应该包括书名、作者和ISBN等属性，并且能够实现借出和归还书籍的方法。
// Library类应该包括一个可用书籍列表和一个已借出书籍列表，以及能够添加新书、借出书籍和归还书籍的方法。（apply、unapply方法必须使用）
class Book(val title: String, val author: String, val isbn: String) {
  private var borrowed: Boolean = false
  
  def borrow(): Boolean = {
    if (!borrowed) {
      borrowed = true
      true
    } else {
      false
    }
  }
  
  def returnBook(): Boolean = {
    if (borrowed) {
      borrowed = false
      true
    } else {
      false
    }
  }
}

class Library {
  var availableBooks: List[Book] = List()
  var borrowedBooks: List[Book] = List()
  
  def apply(book: Book): Unit = {
    availableBooks = book :: availableBooks
  }
  
  def unapply(book: Book): Unit = {
    availableBooks = availableBooks.filterNot(_ == book)
    borrowedBooks = borrowedBooks.filterNot(_ == book)
  }
  
  def borrow(book: Book): Boolean = {
    if (book.borrow()) {
      availableBooks = availableBooks.filterNot(_ == book)
      borrowedBooks = book :: borrowedBooks
      true
    } else {
      false
    }
  }
  
  def returnBook(book: Book): Boolean = {
    if (book.returnBook()) {
      borrowedBooks = borrowedBooks.filterNot(_ == book)
      availableBooks = book :: availableBooks
      true
    } else {
      false
    }
  }
}

// 创建图书和图书馆实例
val book1 = new Book("Python for Data Analysis", "Wes McKinney", "978-1491957660")
val book2 = new Book("Hands-On Machine Learning with Scikit-Learn and TensorFlow", "Aurélien Géron", "978-1491962299")
val book3 = new Book("The Elements of Statistical Learning", "Trevor Hastie, Robert Tibshirani, Jerome Friedman", "978-0387848570")
val library = new Library()

// 添加图书到图书馆
library.apply(book1)
library.apply(book2)
library.apply(book3)

// 借阅一本书
library.borrow(book1)

// 归还一本书
library.returnBook(book1)

// 输出当前可用和已借出的书籍列表
println("Available books:")
library.availableBooks.foreach(book => println(book.title))
println("Borrowed books:")
library.borrowedBooks.foreach(book => println(book.title))

// Available books:
// Python for Data Analysis
// Hands-On Machine Learning with Scikit-Learn and TensorFlow
// The Elements of Statistical Learning
// Borrowed books:
